package cn.yunhe.controller;

import cn.yunhe.entity.User;
import cn.yunhe.pojo.UserQuestionHisErrorprac;
import cn.yunhe.service.UserQuestionHisErrorpracService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserQuestionHisErrorPracController {
    @Autowired
    @Qualifier("userQuestionHisErrorpracService")
    private UserQuestionHisErrorpracService userQuestionHisErrorpracService;

    //这个用几乎进入错误练习的页面：试题练习-》错题练习-》参加练习
//
//    @RequestMapping("/intoerrorpractice")
//    public Map intoUserQuestionHisErrorPrac(HttpServletRequest request, HttpSession session){
//        User user = (User) session.getAttribute("Curr_User");
//        int fieldid = (int) session.getAttribute("priafieldId");
//        int qyid = (int) session.getAttribute("qyid");
//        List<UserQuestionHisErrorprac> userQuestionHisErrorpracs = userQuestionHisErrorpracService.getQuesHisErrorPrac(user,fieldid,qyid);
//        int allTotle =  userQuestionHisErrorpracService.getAllErrorTitle(user,fieldid,qyid);
//        Map map = new HashMap();
//        map.put("userQuestionHisErrorpracs",userQuestionHisErrorpracs);
//        map.put("allTotle",allTotle);
//        return map;
//    }

    @RequestMapping("/intoerrorpractice")
    public ModelAndView intoerrorpractice(HttpServletRequest request, HttpSession session){
        ModelAndView mv = new ModelAndView();
        int qyid = Integer.parseInt(request.getParameter("qyid"));
        session.setAttribute("qyid",qyid);
        mv.setViewName("practice-error-improve");
        return mv;
    }

    //这个用几乎进入错误练习的页面：试题练习-》错题练习-》参加练习
    @RequestMapping("/intoerrorpractice2")
    public Map<String,Object> intoUserQuestionHisErrorPrac(HttpServletRequest request, HttpSession session){
        User user = (User) session.getAttribute("Curr_User");
        int fieldid = (int) session.getAttribute("priafieldId");
        int qyid = (int) session.getAttribute("qyid");
        int curPage = Integer.parseInt(request.getParameter("curPage"));//当前的页码
        int perTotle = Integer.parseInt(request.getParameter("perTotle"));//每一页的数量
        PageInfo page = userQuestionHisErrorpracService.pageSplit(user,fieldid,qyid,curPage,perTotle);
        String qyname =  userQuestionHisErrorpracService.getQyNameById(qyid);
        String fieldname = userQuestionHisErrorpracService.getfieldNameById(fieldid);
        Map<String,Object> map  = new HashMap();
        map.put("fieldname",fieldname);
        map.put("qyname",qyname);
        map.put("userQuestionHisErrorpracs",page);
        map.put("allTotle",page.getPages());
        return map;
    }
    //用户练习结果保存,you的话进行更新，没有的话进行增加
    @RequestMapping("/usersavepractice")
    public String usersavepractice(HttpServletRequest request,HttpSession session){
        String data1 = request.getParameter("data1");
        User user = (User) session.getAttribute("Curr_User");
        int qyid = (int) session.getAttribute("qyid");
        boolean b = userQuestionHisErrorpracService.insertpracanswer(user.getUserId(),qyid,data1);
        System.out.println(b);
        return "ss";
    }
}
